/*==============================================================================
*	File Name                 Bsp_USART.c
*	Functional Description    USART function Control
*	Other					  be used to serial port debugging
*   Date                      2025/10/30
*	Author                    LIAI.
==============================================================================*/

#include "Bsp_USART.h"
#include "stdio.h"

//------------------------------------------------------------------------------
//	macro define （宏定义）
//------------------------------------------------------------------------------
#ifdef _RAISONANCE_
#define PUTCHAR_PROTOTYPE int putchar (char c)
#define GETCHAR_PROTOTYPE int getchar (void)
#elif defined (_COSMIC_)
#define PUTCHAR_PROTOTYPE char putchar (char c)
#define GETCHAR_PROTOTYPE char getchar (void)
#else /* _IAR_ */
#define PUTCHAR_PROTOTYPE int putchar (int c)
#define GETCHAR_PROTOTYPE int getchar (void)
#endif /* _RAISONANCE_ */

//------------------------------------------------------------------------------
//	function declaration（函数声明）
//------------------------------------------------------------------------------
void Bsp_Usart1Init(void);

//------------------------------------------------------------------------------
//	para define（变量定义）
//------------------------------------------------------------------------------


//------------------------------------------------------------------------------
//	function define （函数定义）
//------------------------------------------------------------------------------
/*------------------------------------------------------------------------------
*   @name    	Bsp_Usart1Init
*   @brief		USART1  初始化
*   @param[in]	void
*   @return		void
*   @attention	底层GPIO、USART1初始化只配置为115200/PA3->TX
------------------------------------------------------------------------------*/
void Bsp_Usart1Init(void)
{

  GPIO_Init(USART1_PRINT_PORT, USART1_PRINT_PIN, GPIO_MODE_OUT_PP_HIGH_FAST);

  UART1_DeInit();
  UART1_Init(115200, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO,
             UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TX_ENABLE);
}

/*------------------------------------------------------------------------------
*   @brief		将C库printf函数重定向到UART
*   @param[in]	c 要发送的字符
*   @return		char 已发送的字符
------------------------------------------------------------------------------*/
PUTCHAR_PROTOTYPE
{
  /* Write a character to the UART1 */
  UART1_SendData8(c);
  /* Loop until the end of transmission */
  while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET);

  return (c);
}

/*------------------------------------------------------------------------------
*   @brief		将C库scanf函数重定向到USART
*   @param[in]	None
*   @return		char 要读取的字符
------------------------------------------------------------------------------*/
GETCHAR_PROTOTYPE
{
#ifdef _COSMIC_
  char c = 0;
#else
  int c = 0;
#endif
  /* Loop until the Read data register flag is SET */
  while (UART1_GetFlagStatus(UART1_FLAG_RXNE) == RESET);
    c = UART1_ReceiveData8();
  return (c);
}